<?php
/**
 * Created by PhpStorm.
 * User: roguefeathers
 * Date: 2018-9-6
 * Time: 17:15
 */

header('Content-Type: text/html; charset=utf-8');
require_once ("./05page.class.php");

//页面基本参数初始化
$pageSize = 5; //每一页显示多少行数的数据
$showpage = 5; //分页条显示几个指定按钮


//1、传入页码
$page = isset($_GET['p']) ? $_GET['p'] : 1;
$url = "04AjaxPage.php?p={page}";
//一页显示多少条记录

//2、根据页码取出数据：php->mysql处理
try{
    //PDO链接数据库
    $pdo = new PDO('mysql:host=localhost; dbname=phptest', 'root', 'root');
    //执行的查询语句
    //编写sql获取分页数据SLECT * FROM 表名 LIMIT 起始位置，显示条数
    $sql = "select * from computermenu LIMIT ".(($page-1)*$pageSize).",".$pageSize;
    //$sql = "select * from computermenu";
    //把sql语句传送到数据库
    $stmt = $pdo->query($sql);

    echo <<<eof
        <style type="text/css">
            a:link, a:visited, a:hover; a:active{
                color: #000000;
            }
            table{
                width: 850px;
                margin: auto;   
                border: 1px solid black;
                border-collapse: collapse;
            }
            td{
                border: 1px solid black;
                text-align: center;
            }
            
            #page{
                height:50px;
                padding:0px 0px;
                display: inline-block;
            }
            #page a{
                display:block;
                float:left;
                margin: 12px 3px;
                padding:2px 12px;
                height:24px;
                border:1px #cccccc solid;
                background:#fff;
                text-decoration:none;
                color:#808080;
                font-size:12px;
                line-height:24px;
            }
            #page a:hover{
                color:#077ee3;
                border:1px #077ee3 solid;
            }
            #page a.cur{
                border:none;
                background:#077ee3;
                color:#fff;
            }
            #page p{
                float:left;
                padding:2px 12px;
                font-size:12px;
                height:24px;
                line-height:24px;
                color:#bbb;
                border:1px #ccc solid;
                background:#fcfcfc;
                margin-right:8px;
              
            }
            #page p.pageRemark{
                border-style:none;
                background:none;
                margin-right:0px;
                padding:4px 0px;
                color:#666;
            }
            #page p.pageRemark b{
                color:red;
            }
            #page p.pageEllipsis{
                color:#808080;
              
            }
        </style>
        <table>
            <tr style="background-color: #e6ffec;">
                <td>序号</td>
                <td>电脑名称</td>
                <td>电脑颜色</td>
                <td>价格</td>
            </tr>
eof;


    foreach($stmt as $row){
        echo '<tr>';
        echo '<td>'.$row['id'].'</td>';
        echo '<td>'.$row['computername'].'</td>';
        echo '<td>'.$row['computercolor'].'</td>';
        echo '<td>'.$row['computerprice'].'</td>';
        echo '</tr>';
    }

    //3、显示数据+分页条
    //获取当前php当前文件
    $self = $_SERVER['PHP_SELF'];
    //查询数据库中数据的条数
    $total_sql = 'select * from computermenu';
    $total_stmt = $pdo->query($total_sql);
    $total = $total_stmt->rowCount();


    //计算页数
    $total_page = ceil($total/$pageSize);
    echo '<tr><td colspan="4">';
    if($total > $pageSize){
        $page = new page($total, $pageSize, $page, $url, 2);
        echo $page->pg_write();
    }
    echo '</td></tr>';
    echo '</table>';

}catch(PDOException $e){
    echo $e->getMessage();
}
